Skip to content

Log the source of every configuration key - #1389

Merged
benoit-cty merged 1 commit into
mlco2:masterfrom
NoiceHax:fix/issue-312
Aug 28, 2026
Merged

Log the source of every configuration key#1389
benoit-cty merged 1 commit into
mlco2:masterfrom
NoiceHax:fix/issue-312

Conversation

@NoiceHax

Copy link
Copy Markdown
Contributor

Description

get_hierarchical_config only logged which .codecarbon.config files it had read, so there was still no way to tell which layer set a given key. It now reads the global file, the local file and the CODECARBON_ environment variables into separate dicts, merges them in the same order as before, and keeps a key to source map next to the result. The two file level messages are replaced by one line naming the source of each resolved key, with environment variables shown as CODECARBON_<KEY>.

Only key names and their source are logged, never the values, because api_key and experiment_id live in this config space.

Example output:

[codecarbon INFO @ 09:30:00] Codecarbon configuration: api_key from /home/victor/.codecarbon.config, experiment_id from CODECARBON_EXPERIMENT_ID, measure_power_secs from /home/victor/.codecarbon.config

Related Issue

Closes #312

Motivation and Context

Config comes from three places that silently override each other. When a value was not what you expected, nothing in the log pointed at the file or the variable responsible. That is what the issue asks for, for example knowing where experiment_id was picked up from.

How Has This Been Tested?

Two tests added to tests/test_config.py. The first checks the key to source map for a key set only in the global file, a key overridden by the local file, and a key overridden by an environment variable. The second checks the log line names the sources and does not contain the value of api_key.

Ran CODECARBON_ALLOW_MULTIPLE_RUNS=True pytest tests/test_config.py tests/test_resource_tracker.py tests/cli, 114 passed. The full package suite gives 627 passed with one failure in test_task_energy_with_live_update_interference, which fails the same way on master on my machine, so it does not come from this change.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

AI Usage Disclosure

  • 🟥 AI-vibecoded: You cannot explain the logic. Car analogy : the car drive by itself, you are outside it and just tell it where to go.
  • 🟠 AI-generated: Car analogy : the car drive by itself, you are inside and give instructions.
  • â­� AI-assisted. Car analogy : you drive the car, AI help you find your way.
  • â™»ï¸� No AI used. Car analogy : you drive the car.

I used an AI coding agent to write this patch and this description from an approach I decided on, and I reviewed the code and ran the tests locally before opening the PR.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the docs/how-to/contributing.md document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@NoiceHax
NoiceHax requested a review from a team as a code owner August 15, 2026 11:52
get_hierarchical_config only logged which .codecarbon.config files were
read, so there was no way to tell which layer had set a given key.

It now reads each layer on its own and keeps a key to source map while
merging the global file, the local file and the CODECARBON_ environment
variables. The two file level messages are replaced by a single line
naming the source of every resolved key, with environment variables
shown as CODECARBON_<KEY>. Values are never logged, as api_key and
experiment_id live in this config space.
@benoit-cty

Copy link
Copy Markdown
Contributor

Thanks, it's a good feature 👍

@codecov

codecov Bot commented Aug 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.55172% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 91.58%. Comparing base (e3171e8) to head (edd7b0a).

Files with missing lines Patch % Lines
codecarbon/core/config.py 96.55% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1389      +/-   ##
==========================================
+ Coverage   91.55%   91.58%   +0.02%     
==========================================
  Files          49       49              
  Lines        5082     5096      +14     
==========================================
+ Hits         4653     4667      +14     
  Misses        429      429              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@benoit-cty
benoit-cty merged commit 2c1b435 into mlco2:master Aug 28, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add log of config and where they come from

2 participants